package com.sport.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sport.bean.Kit;
import com.sport.bean.PageParam;
import com.sport.service.IKitService;
import com.sport.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class KitController {
    @Autowired
    private IKitService iKitService;

    /*@GetMapping(value = "/admin/list")
    @ResponseBody
    public Map<String, Object> findAll() {
        List<Admin> admins = adminService.findAll();
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("data", admins);
        return map;
    }*/

    @GetMapping(value = "/kit/list")
    @ResponseBody
    public Map findAll1(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "limit", defaultValue = "10") Integer limit,
                                      HttpServletRequest request) {
        //查询，分页器械
        PageHelper.startPage(page, limit);
        Map map = new HashMap();
        PageParam<Kit> pageParam = new PageParam<>(request);
        List<Kit> kits = iKitService.listPage(pageParam);
        PageInfo info = new PageInfo(kits);

        map.put("code",0);
        map.put("msg","");
        map.put("count",info.getTotal());
        map.put("data",kits);
//        PageResult<Kit> (kits, info.getTotal());
        return map;
    }
    /*
      删除
       */
    @GetMapping("/kit/del")
    @ResponseBody
    public JsonResult delKit(Integer id) {
        if (iKitService.deletedKit(id) > 0) {
            return JsonResult.ok("删除成功");
        } else {
            return JsonResult.error("删除失败");
        }
    }


    @GetMapping("/kit/findId")
    @ResponseBody
    public Kit findId(Integer id) {
        return iKitService.findOneById(id);
    }


    /*
  修改kit
   */
    @GetMapping("/kit/update")
    @ResponseBody
    public JsonResult updatAdmin(MultipartFile file, Kit kit) {
        if ( iKitService.updateKit(kit) > 0) {
            return JsonResult.ok("修改成功");
        } else {
            return JsonResult.error("修改成功");
        }

    }

}